const init = (fun) => {
  Vue.onMounted(() => {
    fun && fun();
  });
};

// 普通表格
const useTable = function () {
  return Vue.reactive({
    loading: false,
    data: [],
    totalRow: 0,
  });
};

const req = function (request, url, requestData, callback, loading) {
  request(url, Vue.unref(requestData), loading)
    .then(function (res) {
      callback && callback(res);
    })
    .catch(function (err) {});
};

const $fetch = function (request, url, requestData, data, callback, loading) {
  request(url, Vue.unref(requestData), loading)
    .then(function (res) {
      if (res.code === 200 && data) {
        Vue.isRef(data) ? (data.value = res.data) : (data = res.data);
      }
      callback && callback(res);
    })
    .catch(function (err) {});
};

function pageCount(total, limit) {
  return total > 0 ? (total < limit ? 1 : total % limit ? parseInt(total / limit) + 1 : total / limit) : 0;
}

/**
 * 表格查询
 * @param {*} request  请求方法
 * @param {string} url 请求地址
 * @param {object} requestData  请求数据
 * @param {*} table  表格
 * @param {Function}  callback 回调方法
 */
const fetchPage = function (request, url, requestData, table, callback) {
  table.loading = true;

  request(url, Vue.unref(requestData))
    .then(function (res) {
      if (res.code === 200) {
        const { records, totalRow, pageNumber } = res.data;
        if (records.length === 0 && pageNumber > 1) {
          requestData.pageNumber = pageNumber - 1;
          fetchPage(request, url, requestData, table, callback);
        } else {
          table.data = records;
          table.totalRow = totalRow;
          if (Vue.unref(requestData).totalRow) {
            Vue.isRef(requestData) ? (requestData.value.totalRow = res.data.totalRow) : (requestData.totalRow = res.data.totalRow);
          }
          if (callback) callback(res);
        }
      } else {
        window.$baseAlert(res.msg);
      }
      table.loading = false;
    })
    .catch(function (err) {
      table.loading = false;
      window.$baseAlert(err);
    });
};

/**
 * 列表查询
 * @param {*} request  请求方法
 * @param {string} url 请求地址
 * @param {object} requestData  请求数据
 * @param {*} table  表格
 * @param {function} callback 回调函数
 */
const fetchList = function (request, url, requestData, table, callback) {
  table.loading = true;
  request(url, Vue.unref(requestData), table?.loading)
    .then(function (res) {
      if (res.code === 200) {
        table.data = res.data;
        if (callback) callback(res);
      } else {
        window.$baseAlert(res.msg);
      }
      table.loading = false;
    })
    .catch(function (err) {
      table.loading = false;
      window.$baseAlert(err);
    });
};

// 树形表格
const useTreeTable = function (loading) {
  return Vue.reactive({
    loading: loading || false,
    data: [],
    fixedData: [],
  });
};

/**
 * 树形表格查询
 * @param {*} request  请求方法
 * @param {string} url 请求地址
 * @param {object} requestData  请求数据
 * @param {*} treeTable  树形表格
 * @param {function} callback 回调函数
 */
const fetchTree = function (request, url, requestData, treeTable, callback) {
  treeTable.loading = true;
  request(url, Vue.unref(requestData), treeTable?.loading)
    .then(function (res) {
      if (res.code === 200) {
        treeTable.data = res.data;
        treeTable.fixedData = res.data;
        if (callback) callback();
      } else {
        window.$baseAlert(res.msg);
      }
      treeTable.loading = false;
    })
    .catch(function (err) {
      treeTable.loading = false;
      window.$baseAlert(err);
    });
};

/**
 * 移动
 * @param {*} request  请求方法
 * @param {string} url 请求地址
 * @param {object} requestData  请求数据
 * @param {*} treeTable  表格
 * @param {Function} callback 回调函数
 */
const handleMove = function (request, url, requestData, treeTable, callback) {
  treeTable.loading = true;
  request(url, Vue.unref(requestData), treeTable.loading)
    .then(function (res) {
      if (res.code === 200) {
        window.$baseMsg(res.msg, { icon: 1 });
        if (callback) callback();
      } else {
        window.$baseAlert(res.msg);
      }
      treeTable.loading = false;
    })
    .catch(function (err) {
      treeTable.loading = false;
      window.$baseAlert(err);
    });
};
/**
 * 删除操作
 * @param {*} request  删除的请求方法
 * @param {string} url 请求地址
 * @param {object} requestData  删除请求数据
 * @param {*} table  表格
 * @param {*} message 提示文字
 * @param {*} callback 回调函数
 */
const handleDelete = function (request, url, requestData, table, message, callback) {
  if (requestData) {
    window.$baseConfirm(message || '你确定要删除吗？', '系统提示', function () {
      table.loading = true;
      request(url, Vue.unref(requestData), table?.loading)
        .then(function (res) {
          if (res.code === 200) {
            window.$baseMsg(res.msg, { icon: 1 });
            if (callback) callback(res);
          } else {
            window.$baseAlert(res.msg);
          }
          table.loading = false;
        })
        .catch(function (err) {
          table.loading = false;
          window.$baseAlert(err);
        });
    });
  } else {
    table.loading = false;
    window.$baseMsg('请传入删除信息', { icon: 2 });
  }
};

const uploadFile = (data) => {
  return axiosPost('common/file/upload', data);
};
